package com.qipay.baas.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.qipay.baas.model.CpWithdrawTradlog;
import com.qipay.baas.model.CpWithdrawTradlogExample.Criteria;
import com.qipay.baas.model.CpWithdrawTradlogExample.Criterion;
import com.qipay.baas.model.CpWithdrawTradlogExample;
import java.util.List;
import java.util.Map;

public class CpWithdrawTradlogSqlProvider {

    public String deleteByExample(CpWithdrawTradlogExample example) {
        BEGIN();
        DELETE_FROM("cp_withdraw_tradlog");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(CpWithdrawTradlog record) {
        BEGIN();
        INSERT_INTO("cp_withdraw_tradlog");
        
        if (record.getCpId() != null) {
            VALUES("cp_id", "#{cpId,jdbcType=BIGINT}");
        }
        
        if (record.getAmount() != null) {
            VALUES("amount", "#{amount,jdbcType=INTEGER}");
        }
        
        if (record.getFee() != null) {
            VALUES("fee", "#{fee,jdbcType=DECIMAL}");
        }
        
        if (record.getActualAmount() != null) {
            VALUES("actual_amount", "#{actualAmount,jdbcType=INTEGER}");
        }
        
        if (record.getType() != null) {
            VALUES("`type`", "#{type,jdbcType=TINYINT}");
        }
        
        if (record.getCardBank() != null) {
            VALUES("card_bank", "#{cardBank,jdbcType=VARCHAR}");
        }
        
        if (record.getCardSubBank() != null) {
            VALUES("card_sub_bank", "#{cardSubBank,jdbcType=VARCHAR}");
        }
        
        if (record.getCardBankNo() != null) {
            VALUES("card_bank_no", "#{cardBankNo,jdbcType=VARCHAR}");
        }
        
        if (record.getCardNo() != null) {
            VALUES("card_no", "#{cardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getIdCardNo() != null) {
            VALUES("id_card_no", "#{idCardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            VALUES("`name`", "#{name,jdbcType=VARCHAR}");
        }
        
        if (record.getMobile() != null) {
            VALUES("mobile", "#{mobile,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeNo() != null) {
            VALUES("trade_no", "#{tradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getOutTradeNo() != null) {
            VALUES("out_trade_no", "#{outTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getAttach() != null) {
            VALUES("attach", "#{attach,jdbcType=VARCHAR}");
        }
        
        if (record.getRemark() != null) {
            VALUES("remark", "#{remark,jdbcType=VARCHAR}");
        }
        
        if (record.getState() != null) {
            VALUES("`state`", "#{state,jdbcType=TINYINT}");
        }
        
        if (record.getOperation() != null) {
            VALUES("`operation`", "#{operation,jdbcType=TINYINT}");
        }
        
        if (record.getCreateTime() != null) {
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFinishTime() != null) {
            VALUES("finish_time", "#{finishTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getProvince() != null) {
            VALUES("province", "#{province,jdbcType=VARCHAR}");
        }
        
        if (record.getCity() != null) {
            VALUES("city", "#{city,jdbcType=VARCHAR}");
        }
        
        if (record.getArea() != null) {
            VALUES("area", "#{area,jdbcType=VARCHAR}");
        }
        
        if (record.getHasremind() != null) {
            VALUES("hasRemind", "#{hasremind,jdbcType=TINYINT}");
        }
        
        if (record.getIsCompany() != null) {
            VALUES("is_company", "#{isCompany,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    public String selectByExample(CpWithdrawTradlogExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("cp_id");
        SELECT("amount");
        SELECT("fee");
        SELECT("actual_amount");
        SELECT("`type`");
        SELECT("card_bank");
        SELECT("card_sub_bank");
        SELECT("card_bank_no");
        SELECT("card_no");
        SELECT("id_card_no");
        SELECT("`name`");
        SELECT("mobile");
        SELECT("trade_no");
        SELECT("out_trade_no");
        SELECT("attach");
        SELECT("remark");
        SELECT("`state`");
        SELECT("`operation`");
        SELECT("create_time");
        SELECT("finish_time");
        SELECT("province");
        SELECT("city");
        SELECT("area");
        SELECT("hasRemind");
        SELECT("is_company");
        FROM("cp_withdraw_tradlog");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        CpWithdrawTradlog record = (CpWithdrawTradlog) parameter.get("record");
        CpWithdrawTradlogExample example = (CpWithdrawTradlogExample) parameter.get("example");
        
        BEGIN();
        UPDATE("cp_withdraw_tradlog");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getCpId() != null) {
            SET("cp_id = #{record.cpId,jdbcType=BIGINT}");
        }
        
        if (record.getAmount() != null) {
            SET("amount = #{record.amount,jdbcType=INTEGER}");
        }
        
        if (record.getFee() != null) {
            SET("fee = #{record.fee,jdbcType=DECIMAL}");
        }
        
        if (record.getActualAmount() != null) {
            SET("actual_amount = #{record.actualAmount,jdbcType=INTEGER}");
        }
        
        if (record.getType() != null) {
            SET("`type` = #{record.type,jdbcType=TINYINT}");
        }
        
        if (record.getCardBank() != null) {
            SET("card_bank = #{record.cardBank,jdbcType=VARCHAR}");
        }
        
        if (record.getCardSubBank() != null) {
            SET("card_sub_bank = #{record.cardSubBank,jdbcType=VARCHAR}");
        }
        
        if (record.getCardBankNo() != null) {
            SET("card_bank_no = #{record.cardBankNo,jdbcType=VARCHAR}");
        }
        
        if (record.getCardNo() != null) {
            SET("card_no = #{record.cardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getIdCardNo() != null) {
            SET("id_card_no = #{record.idCardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            SET("`name` = #{record.name,jdbcType=VARCHAR}");
        }
        
        if (record.getMobile() != null) {
            SET("mobile = #{record.mobile,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeNo() != null) {
            SET("trade_no = #{record.tradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getOutTradeNo() != null) {
            SET("out_trade_no = #{record.outTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getAttach() != null) {
            SET("attach = #{record.attach,jdbcType=VARCHAR}");
        }
        
        if (record.getRemark() != null) {
            SET("remark = #{record.remark,jdbcType=VARCHAR}");
        }
        
        if (record.getState() != null) {
            SET("`state` = #{record.state,jdbcType=TINYINT}");
        }
        
        if (record.getOperation() != null) {
            SET("`operation` = #{record.operation,jdbcType=TINYINT}");
        }
        
        if (record.getCreateTime() != null) {
            SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFinishTime() != null) {
            SET("finish_time = #{record.finishTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getProvince() != null) {
            SET("province = #{record.province,jdbcType=VARCHAR}");
        }
        
        if (record.getCity() != null) {
            SET("city = #{record.city,jdbcType=VARCHAR}");
        }
        
        if (record.getArea() != null) {
            SET("area = #{record.area,jdbcType=VARCHAR}");
        }
        
        if (record.getHasremind() != null) {
            SET("hasRemind = #{record.hasremind,jdbcType=TINYINT}");
        }
        
        if (record.getIsCompany() != null) {
            SET("is_company = #{record.isCompany,jdbcType=VARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("cp_withdraw_tradlog");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("cp_id = #{record.cpId,jdbcType=BIGINT}");
        SET("amount = #{record.amount,jdbcType=INTEGER}");
        SET("fee = #{record.fee,jdbcType=DECIMAL}");
        SET("actual_amount = #{record.actualAmount,jdbcType=INTEGER}");
        SET("`type` = #{record.type,jdbcType=TINYINT}");
        SET("card_bank = #{record.cardBank,jdbcType=VARCHAR}");
        SET("card_sub_bank = #{record.cardSubBank,jdbcType=VARCHAR}");
        SET("card_bank_no = #{record.cardBankNo,jdbcType=VARCHAR}");
        SET("card_no = #{record.cardNo,jdbcType=VARCHAR}");
        SET("id_card_no = #{record.idCardNo,jdbcType=VARCHAR}");
        SET("`name` = #{record.name,jdbcType=VARCHAR}");
        SET("mobile = #{record.mobile,jdbcType=VARCHAR}");
        SET("trade_no = #{record.tradeNo,jdbcType=VARCHAR}");
        SET("out_trade_no = #{record.outTradeNo,jdbcType=VARCHAR}");
        SET("attach = #{record.attach,jdbcType=VARCHAR}");
        SET("remark = #{record.remark,jdbcType=VARCHAR}");
        SET("`state` = #{record.state,jdbcType=TINYINT}");
        SET("`operation` = #{record.operation,jdbcType=TINYINT}");
        SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        SET("finish_time = #{record.finishTime,jdbcType=TIMESTAMP}");
        SET("province = #{record.province,jdbcType=VARCHAR}");
        SET("city = #{record.city,jdbcType=VARCHAR}");
        SET("area = #{record.area,jdbcType=VARCHAR}");
        SET("hasRemind = #{record.hasremind,jdbcType=TINYINT}");
        SET("is_company = #{record.isCompany,jdbcType=VARCHAR}");
        
        CpWithdrawTradlogExample example = (CpWithdrawTradlogExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(CpWithdrawTradlog record) {
        BEGIN();
        UPDATE("cp_withdraw_tradlog");
        
        if (record.getCpId() != null) {
            SET("cp_id = #{cpId,jdbcType=BIGINT}");
        }
        
        if (record.getAmount() != null) {
            SET("amount = #{amount,jdbcType=INTEGER}");
        }
        
        if (record.getFee() != null) {
            SET("fee = #{fee,jdbcType=DECIMAL}");
        }
        
        if (record.getActualAmount() != null) {
            SET("actual_amount = #{actualAmount,jdbcType=INTEGER}");
        }
        
        if (record.getType() != null) {
            SET("`type` = #{type,jdbcType=TINYINT}");
        }
        
        if (record.getCardBank() != null) {
            SET("card_bank = #{cardBank,jdbcType=VARCHAR}");
        }
        
        if (record.getCardSubBank() != null) {
            SET("card_sub_bank = #{cardSubBank,jdbcType=VARCHAR}");
        }
        
        if (record.getCardBankNo() != null) {
            SET("card_bank_no = #{cardBankNo,jdbcType=VARCHAR}");
        }
        
        if (record.getCardNo() != null) {
            SET("card_no = #{cardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getIdCardNo() != null) {
            SET("id_card_no = #{idCardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            SET("`name` = #{name,jdbcType=VARCHAR}");
        }
        
        if (record.getMobile() != null) {
            SET("mobile = #{mobile,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeNo() != null) {
            SET("trade_no = #{tradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getOutTradeNo() != null) {
            SET("out_trade_no = #{outTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getAttach() != null) {
            SET("attach = #{attach,jdbcType=VARCHAR}");
        }
        
        if (record.getRemark() != null) {
            SET("remark = #{remark,jdbcType=VARCHAR}");
        }
        
        if (record.getState() != null) {
            SET("`state` = #{state,jdbcType=TINYINT}");
        }
        
        if (record.getOperation() != null) {
            SET("`operation` = #{operation,jdbcType=TINYINT}");
        }
        
        if (record.getCreateTime() != null) {
            SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFinishTime() != null) {
            SET("finish_time = #{finishTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getProvince() != null) {
            SET("province = #{province,jdbcType=VARCHAR}");
        }
        
        if (record.getCity() != null) {
            SET("city = #{city,jdbcType=VARCHAR}");
        }
        
        if (record.getArea() != null) {
            SET("area = #{area,jdbcType=VARCHAR}");
        }
        
        if (record.getHasremind() != null) {
            SET("hasRemind = #{hasremind,jdbcType=TINYINT}");
        }
        
        if (record.getIsCompany() != null) {
            SET("is_company = #{isCompany,jdbcType=VARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(CpWithdrawTradlogExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}